package edu.uafs.museum;

import java.net.HttpURLConnection;
import java.net.URL;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.support.v4.app.NavUtils;

public class ExhibitsMain extends Activity {
	public static final String EXHIBITS_XML_URL = "edu.uafs.museum.exhibits_xml";
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_exhibits_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_exhibits_main, menu);
        return true;
    }

    /**
     * This is the handler of the search button
     * @param v
     */
    public void searchHandler(View v)
    {
    	//get the id
    	String id = ((EditText)findViewById(R.id.idTextBox)).getText().toString();
    	
    	//check the availability of the url
    	try {
    		String urlString = convertIdToURL(id);
    	      HttpURLConnection.setFollowRedirects(false);
    	      HttpURLConnection con = (HttpURLConnection)new URL(urlString).openConnection();
    	      con.setInstanceFollowRedirects(false);
    	      con.setRequestMethod("HEAD");
    	      
    	      //if the url is available
    	      if (con.getResponseCode() == HttpURLConnection.HTTP_OK)
    	      {
    	    	  //let the user view the exhibit
    	    	  Intent intent = new Intent(this, DisplayExhibitActivity.class);
    	    	  intent.putExtra(DisplayExhibitsActivity.KEY_LINK, urlString);
    	    	  startActivity(intent);
    	      }
    	      else
    	      {
    	    	  //if the url is not available, display error message using a Toast
    	    	  Context context = getApplicationContext();
    	    	  CharSequence text = getResources().getString(R.string.invalid_exhibit_id);
    	    	  int duration = Toast.LENGTH_SHORT;

    	    	  Toast toast = Toast.makeText(context, text, duration);
    	    	  toast.show();
    	      }
    	    }
    	    catch (Exception e) {
    	       e.printStackTrace();
    	    }
    }
    
    /**
     * Handler of list all exhibits button
     * @param v
     */
    public void listHandler(View v)
    {
		Intent intent = new Intent(getApplicationContext(), DisplayExhibitsActivity.class);
		intent.putExtra(EXHIBITS_XML_URL,getResources().getString(R.string.exhibits_xml));
		startActivity(intent);
    }
    
    /**
     * Call the QR Code scanner and process the result. This is the handler of scan QR code button
     * @param v
     */
    public void scanQRCode(View v)
    {
    	Intent intent = new Intent("com.google.zxing.client.android.SCAN");
    	intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
    	startActivityForResult(intent, 0);
    }
    
    /**
     * Convert the id of the exhibit into the url string
     * @param id The id of the exhibit
     * @return The url string
     */
    private String convertIdToURL(String id)
    {
    	return getResources().getString(R.string.host_string) + "exhibitions/" + id + ".htm";
    }

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		if (requestCode == 0)
		{
			if (resultCode == RESULT_OK)
			{
				String contents = data.getStringExtra("SCAN_RESULT");
				String format = data.getStringExtra("SCAN_RESULT_FORMAT");
				
				//handle the result
				try {
		    		  String urlString = convertIdToURL(contents);
		    	      HttpURLConnection.setFollowRedirects(false);
		    	      HttpURLConnection con = (HttpURLConnection)new URL(urlString).openConnection();
		    	      con.setInstanceFollowRedirects(false);
		    	      con.setRequestMethod("HEAD");
		    	      
		    	      //if the url is available
		    	      if (con.getResponseCode() == HttpURLConnection.HTTP_OK)
		    	      {
		    	    	  //let the user view the exhibit
		    	    	  Intent intent = new Intent(this, DisplayExhibitActivity.class);
		    	    	  intent.putExtra(DisplayExhibitsActivity.KEY_LINK, urlString);
		    	    	  startActivity(intent);
		    	      }
		    	      else
		    	      {
		    	    	  //if the url is not available, display error message using a Toast
		    	    	  Context context = getApplicationContext();
		    	    	  CharSequence text = getResources().getString(R.string.invalid_exhibit_id);
		    	    	  int duration = Toast.LENGTH_SHORT;

		    	    	  Toast toast = Toast.makeText(context, text, duration);
		    	    	  toast.show();
		    	      }
		    	    }
		    	    catch (Exception e) {
		    	       e.printStackTrace();
		    	    }
				
			}
			else if (resultCode == RESULT_CANCELED)
			{
				//handle cancel
			}
		}
	}
}
